<template>
  <div class="process">
    <filtration
      :ftype="ftype"
      :msg="tacitly"
      :table-header="tableHeader1"
      :select-data="selectData"
      @seachData="handleQuerySonClass"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass()"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQuerySonClass()"
        >
          {{ $t('btn.search') }}
        </el-button>
        <el-button
          type="primary"
          size="mini"
          style="margin-left: 0"
          @click="handleExport"
        >
          导出Excel
        </el-button>
        <el-button type="danger" size="mini" @click="deleteSponge()">
          删除
        </el-button>
        <!--        <el-button type="primary" size="mini" @click="handleExport">下载模板文档</el-button>-->
        <!--        <el-upload-->
        <!--          class="upload-demo"-->
        <!--          action="https://jsonplaceholder.typicode.com/posts/"-->
        <!--          :on-success="handlePreview"-->
        <!--          :on-error="handleRemove"-->
        <!--        >-->
        <!--          <el-button-->
        <!--            style="margin-left: 10px;"-->
        <!--            size="mini"-->
        <!--            type="primary"-->
        <!--          >上传文档</el-button>-->
        <!--        </el-upload>-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        ref="dataTable"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        get-row-keys="fid"
        serial
        expand
        @selectionChange="selectionData"
      >
        <el-table-column
          type="selection"
          width="30px"
          sortable="true"
          align="center"
        />
        <template v-slot:expand="props">
          <el-table :data="props.scope.details" height="auto" class="el-table1">
            <el-table-column
              label="部件名称"
              prop="fpartsName"
              align="center"
            />
            <el-table-column :label="$t('market.fmaterial')" prop="fnumber" align="center" />
            <el-table-column
              :label="$t('shop.fname2')"
              prop="fname"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              :label="$t('market.fmodel')"
              prop="fmodel"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              :label="$t('market.fdescripTion')"
              prop="fdescripTion"
              align="center"
              min-width="200px"
              show-overflow-tooltip
            />
            <el-table-column
              :label="$t('shop.fspecificaTion')"
              prop="fspecificaTion"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column :label="$t('market.funit')" prop="funitName" align="center" />
            <el-table-column label="上底(cm)" prop="ftop" align="center" />
            <el-table-column label="下底(cm)" prop="fout" align="center" />
            <el-table-column
              label="长(cm)*宽(cm)*厚度(cm)"
              prop="fprice"
              align="center"
              min-width="120px"
            >
              <template slot-scope="scope">
                {{ scope.row.flength }}*{{ scope.row.fwidth }}*{{
                  scope.row.fhelght
                }}
              </template>
            </el-table-column>
            <el-table-column :label="$t('market.fqty')" prop="fqty" align="center" />
            <el-table-column label="参数" prop="fparameter" align="center" />
            <el-table-column label="计价用量" prop="fdosage" align="center" />
            <el-table-column :label="$t('market.note')" prop="fnote" align="center" />
          </el-table>
        </template>
        <template v-slot:billSlot="clo">
          <el-link
            type="primary"
            @click="detailPurchase(clo.scope.row.fbillNo)"
          >
            {{ clo.scope.row.fbillNo }}
          </el-link>
        </template>
        <template v-slot:btnState="col">
          <el-tag :type="col.scope.row.staeColor">{{ col.scope.row.fdocumentStatus }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import { querySpongeList, deleteSponge } from '@/api/workshop/spongePants'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { toMxAmina } from '@/components/ToMxamineState'
import filtration from '@/components/filtration'

export default {
  name: 'MarketOrder',
  components: {
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 18,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      activeName: 'first',
      // 表头
      tableData: [], // 工序库数据
      tableHeight: '75vh',
      tableHeader: [
        { label: this.$t('market.cust'), prop: 'fname', minWidth: '100px' },
        {
          label: this.$t('market.coding'),
          type: 'bill',
          prop: 'fbillNo',
          minWidth: '110px'
        },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '90px' },
        {
          label: this.$t('market.fmaterial'),
          prop: 'fnumber',
          minWidth: '110px'
        },
        {
          label: this.$t('shop.fspecificaTion'),
          prop: 'fspecificaTion',
          minWidth: '110px'
        },
        {
          label: this.$t('market.fdescripTion'),
          prop: 'fdescripTion',
          minWidth: '110px'
        },
        {
          label: this.$t('market.creator'),
          prop: 'fcreator',
          minWidth: '110px'
        },
        {
          label: this.$t('market.createDate'),
          prop: 'fcreateDate',
          minWidth: '125px'
        },
        {
          label: this.$t('market.documentstatus'),
          type: 'state',
          prop: 'fdocumentStatus',
          minWidth: '100px'
        }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 220px)' : '75vh'
  },
  methods: {
    // 导出表格-销售订单列表
    handleExport() {
      require.ensure([], () => {
        const tHeader = [
          '海绵库名称',
          '单据编码',
          '物料编码',
          '助记码',
          '物料规格',
          '物料描述',
          '创建人',
          '创建时间',
          '审核状态'
        ]
        const filterVal = [
          'fname',
          'fbillNo',
          'fnumber',
          'fmodel',
          'fspecificaTion',
          'fdescripTion',
          'fcreator',
          'fcreateDate',
          'fdocumentStatus'
        ]
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val.map(item => {
            return toMxAmina(item)
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '海绵库列表')
        } else {
          querySpongeList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return toMxAmina(item)
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '海绵库列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取订单列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await querySpongeList(DATA)
      this.tableData = RES.array.map(item => {
        return toMxAmina(item)
      })
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 订单详情
    detailPurchase(ev) {
      this.$router.push({
        path: '/spongePantsEdit',
        query: {
          fbillNo: ev
        }
      })
    }, // 多选
    selectionData(val) {
      this.val = val
      this.delSpone = []
      val.forEach(item => {
        this.delSpone.push({
          fid: item.fid
        })
      })
    },
    // 删除
    deleteSponge() {
      deleteSponge(this.delSpone).then(res => {
        if (res.code === 0) {
          this.handleGetUntreated()
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 上传成功
    handleRemove(response, file, fileList) {
      this.$message.success('文件上传成功')
    },
    // 上传失败
    handlePreview() {
      this.$message.success('文件上传失败')
    }
  }
}
</script>
<style lang="scss" scoped>
.process {
  @include listBom;
  .el-upload-list {
    display: none !important;
  }
  .header {
    position: relative;

    .header-name {
      width: 100%;
    }

    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
      z-index: 999;
    }
    .btn1 {
      margin-left: 20px;
    }
    &::v-deep .el-upload-list {
      display: none !important;
    }
  }
}
</style>
